﻿using System.Diagnostics;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using mvcincode.Models;

namespace mvcincode.Controllers;

public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        var features = HttpContext.Features;
        return View(features);
    }

    [Authorize(AuthenticationSchemes = "Cookies")]
    // [Authorize(policy: "AtLeast21")]
    [Authorize(policy: "admin")]
    // [Authorize(Roles = "Admin")]
    [Authorize(AuthenticationSchemes = "Bearer")]
    public IActionResult Privacy()
    {
        return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }

    public async Task<IActionResult> Test()
    {
        // 验证登录, 没有登录会跳转到登录界面
        // await HttpContext.ChallengeAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        // return Content("/home/test");

        var authenticateResult = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        if (authenticateResult.Succeeded)
            return Content("Succeed");
        else
            return Content("Failure");

    }
}
